package org.yang.common.core.result;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.yang.common.i18n.enums.RI18nEnum;

/**
 * 基本控制层
 *
 * @author: lslands
 * @version: 1.0
 * @create: 2024/5/16
 * @description:
 */
@Slf4j
public class BaseController {

    /**
     * session 会话
     */
    protected HttpSession session;
    /**
     * HttpRequest
     */
    protected HttpServletRequest request;
    /**
     * HttpResponse
     */
    protected HttpServletResponse response;


    public void  getRequest(HttpServletRequest request,HttpServletResponse response){
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }


    /**
     * 创建返回
     * @param rows 判断
     * @return R
     */
    protected static <T> R<?> toCreateAjax(T rows) {
        if(rows instanceof Boolean){
            return (Boolean) rows? R.success(RI18nEnum.CREATE)
                    : R.fail(RI18nEnum.CREATE_FAIL);
        }else if(rows instanceof Integer){
            return (Integer)rows >0? R.success(RI18nEnum.CREATE)
                    : R.fail(RI18nEnum.CREATE_FAIL);
        }else {
            return R.error();
        }
    }

    /**
     * 更新返回
     * @param rows 判断
     * @return R
     */
    protected static <T> R<?> toUpdateAjax(T rows) {
        if(rows instanceof Boolean){
            return (Boolean) rows? R.success(RI18nEnum.UPDATE)
                    :R.fail(RI18nEnum.UPDATE_FAIL);
        }else if(rows instanceof Integer){
            return (Integer)rows >0? R.success(RI18nEnum.UPDATE)
                    : R.fail(RI18nEnum.UPDATE_FAIL);
        }else {
            return R.error();
        }
    }

    /**
     * 删除返回
     * @param rows 判断
     * @return R
     */
    protected static <T> R<?> toDeleteAjax(T rows) {
        if(rows instanceof Boolean){
            return (Boolean) rows? R.success(RI18nEnum.DELETE):R.fail(RI18nEnum.DELETE_FAIL);
        }else if(rows instanceof Integer){
            return (Integer)rows >0? R.success(RI18nEnum.DELETE) : R.fail(RI18nEnum.DELETE_FAIL);
        }else {
            return R.error();
        }
    }



    /**
     * 上传
     * @param rows 判断
     * @return R
     */
    protected static <T> R<?> toFile(T rows) {
        if(rows instanceof Boolean){
            return (Boolean) rows? R.success(RI18nEnum.UPDATE):R.fail(RI18nEnum.UPDATE_FAIL);
        }else if(rows instanceof Integer){
            return (Integer)rows >0? R.success(RI18nEnum.UPDATE) : R.fail(RI18nEnum.UPDATE_FAIL);
        }else {
            return R.error();
        }
    }

}
